package com.wgchao.amc.mapstruct.mapper.expand;

import com.wgchao.amc.common.ResultCommon;
import com.wgchao.amc.entity.base.*;
import com.wgchao.amc.mapstruct.DTOMapper;
import com.wgchao.amc.mapstruct.dto.SysBaseAgencyDTO;
import com.wgchao.amc.mapstruct.dto.SysBaseGroupDTO;
import com.wgchao.amc.mapstruct.dto.SysBaseRoleDTO;
import com.wgchao.amc.mapstruct.mapper.SysBaseAgencyMapper;
import com.wgchao.amc.mapstruct.mapper.SysBaseGroupMapper;
import com.wgchao.amc.mapstruct.mapper.SysBaseRoleMapper;
import com.wgchao.amc.mapstruct.mapper.SysBaseUserMapper;
import com.wgchao.amc.repository.base.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @ClassName: SysBaseGroupMapperImpl
 * @author: wgchao
 * @createTime: 2019/10/19 11:07 AM
 * @Description: TODO
 */
@Component
public class SysBaseGroupMapperExpand implements DTOMapper<SysBaseGroup, SysBaseGroupDTO> , CommonExpand<SysBaseGroupDTO>{
    @Autowired
    private SysBaseGroupMapper sysBaseGroupMapper;
    @Autowired
    private SysBaseAgencyRepository agencyRepository;
    @Autowired
    private SysBaseAgencyMapper agencyMapper;
    @Autowired
    private SysBaseGroupRepository groupRepository;
    @Autowired
    private SysBaseGrRepository grRepository;
    @Autowired
    private SysBaseRoleRepository roleRepository;
    @Autowired
    private SysBaseRoleMapper roleMapper;
    @Autowired
    private SysBaseUserMapper userMapper;
    @Autowired
    private SysBaseUserRepository userRepository;

    @Override
    public SysBaseGroup toEntity(SysBaseGroupDTO sysBaseGroupDTO) {
        return sysBaseGroupMapper.toEntity(sysBaseGroupDTO);
    }

    @Override
    public SysBaseGroupDTO toDTO(SysBaseGroup sysBaseGroup) {
        return sysBaseGroupMapper.toDTO(sysBaseGroup);
    }

    @Override
    public List<SysBaseGroup> toEntity(List<SysBaseGroupDTO> sysBaseGroupDTOS) {
        return sysBaseGroupMapper.toEntity(sysBaseGroupDTOS);
    }

    @Override
    public List<SysBaseGroupDTO> toDTO(List<SysBaseGroup> sysBaseGroups) {
        return sysBaseGroupMapper.toDTO(sysBaseGroups);
    }

    public SysBaseGroupDTO getAgencyInfo(SysBaseGroupDTO groupDTO){
        if(!StringUtils.isEmpty(groupDTO.getAgencyCode())){
            Optional<SysBaseAgency> agencyOptional = agencyRepository.findById(groupDTO.getAgencyCode());
            if(agencyOptional.isPresent()){
                groupDTO.setAgencyInfo(agencyMapper.toDTO(agencyOptional.get()));
            }
        }
        return groupDTO;
    }

    public SysBaseGroupDTO getParGroup(SysBaseGroupDTO groupDTO){
        if(groupDTO.getParGroup() != null){
            Optional<SysBaseGroup> groupOptional = groupRepository.findById(groupDTO.getParGroup());
            if(groupOptional.isPresent()){
                groupDTO.setParGroupInfo(toDTO(groupOptional.get()));
            }
        }
        return groupDTO;
    }

    public SysBaseGroupDTO getChildGroups(SysBaseGroupDTO groupDTO){
        List<SysBaseGroup> childs = groupRepository.findAllByParGroupAndDelIsFalse(groupDTO.getGroupId());
        List<SysBaseGroupDTO> childDTOs = new ArrayList<>();
        if(childs!=null && !childs.isEmpty()){
            for(SysBaseGroup child : childs){
                SysBaseGroupDTO childDTO = toDTO(child);
                List<SysBaseGroupDTO> cchilds = childGroups(childDTO);
                childDTO.setChildGroupInfos(cchilds);
                childDTOs.add(childDTO);
            }
            groupDTO.setChildGroupInfos(childDTOs);
        }
        return groupDTO;
    }

    private List<SysBaseGroupDTO> childGroups(SysBaseGroupDTO groupDTO){
        List<SysBaseGroup> childs = groupRepository.findAllByParGroupAndDelIsFalse(groupDTO.getGroupId());
        List<SysBaseGroupDTO> childDTOs = new ArrayList<>();
        if(childs!=null && !childs.isEmpty()){
            for(SysBaseGroup child : childs){
                SysBaseGroupDTO childDTO = toDTO(child);
                List<SysBaseGroupDTO> cchilds = childGroups(childDTO);
                childDTO.setChildGroupInfos(cchilds);
                childDTOs.add(childDTO);
            }
            groupDTO.setChildGroupInfos(childDTOs);
        }
        return childDTOs;
    }

    public SysBaseGroupDTO getRoleInfos(SysBaseGroupDTO groupDTO){
        List<SysBaseGr> grList = grRepository.findAllByGroupIdAndDelIsFalse(groupDTO.getGroupId());
        if(grList!=null && !grList.isEmpty()){
            List<SysBaseRoleDTO> roleDTOS = new ArrayList<>();
            for(SysBaseGr gr:grList){
                Optional<SysBaseRole> roleOptional = roleRepository.findById(gr.getRoleId());
                if(roleOptional.isPresent()){
                    SysBaseRole role = roleOptional.get();
                    roleDTOS.add(roleMapper.toDTO(role));
                }
            }
            groupDTO.setRoleInfos(roleDTOS);
        }
        return groupDTO;
    }


    @Override
    public SysBaseGroupDTO getCURDUserInfo(SysBaseGroupDTO groupDTO) {
        if(groupDTO.getCreateUser() != null){
            Optional<SysBaseUser> userOptional = userRepository.findById(groupDTO.getCreateUser());
            if(userOptional.isPresent()){
                groupDTO.setCreateUserInfo(userMapper.toDTO(userOptional.get()));
            }
        }
        if(groupDTO.getLastUpdateUser() != null){
            Optional<SysBaseUser> userOptional = userRepository.findById(groupDTO.getLastUpdateUser());
            if(userOptional.isPresent()){
                groupDTO.setLastUpdateUserInfo(userMapper.toDTO(userOptional.get()));
            }
        }
        if(groupDTO.getDelUser() != null){
            Optional<SysBaseUser> userOptional = userRepository.findById(groupDTO.getDelUser());
            if(userOptional.isPresent()){
                groupDTO.setDelUserInfo(userMapper.toDTO(userOptional.get()));
            }
        }
        return groupDTO;
    }
}
